package demo.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import demo.dao.ReaderDao;
import demo.entity.Reader;

public class ReaderSelect extends JPanel implements ActionListener{
	private JTextField readerId;
	private JTextField readerName;
	private ReaderDao readerDao = new ReaderDao();
	private Reader reader;
	private ReaderPanel readerPanel;
	private JTable table;
	private String[] tableHeader = { "学号", "密码", "姓名", "性别","生日","专业","年级","电话","邮箱" };
	Object[][] readerData = { { "001", "123","张三", "男", "2010-08-06","信管","20","12345678908","12345" }, { "002","123", "李四", "女", "2011-09-09","信管","20","12345678908","12345" } };
	
	public ReaderSelect() {
		setLayout(new BorderLayout(0, 0));

		table = new JTable(readerData, tableHeader);

		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);

		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		/*JLabel lblNewLabel_1 = new JLabel("请输入学号：");
		lblNewLabel_1.setFont(new Font("",Font.BOLD,15));
		panel.add(lblNewLabel_1);
		
		readerId = new JTextField();
		panel.add(readerId);
		readerId.setColumns(10);
		readerId.addActionListener(this);// 为文本框注册Action事件监听器，如果在文本框中按回车键，则该监听器的actionPerformed方法被调用
		
		JButton btnFind1 = new JButton("查找");
		btnFind1.setFont(new Font("",Font.BOLD,15));
		btnFind1.addActionListener(this);//为“查找”按钮注册Action事件监听器，如果该按钮被点击，则该监听器的actionPerformed方法被调用
		panel.add(btnFind1);
		
		btnFind1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//readerPanel.setVisible(true);
				findByIdToTable();
			}
		});*/
		
		JLabel lblNewLabel_2 = new JLabel("请输入姓名：");
		lblNewLabel_2.setFont(new Font("",Font.BOLD,15));
		panel.add(lblNewLabel_2);
		
		readerName = new JTextField();
		panel.add(readerName);
		readerName.setColumns(10);
		readerName.addActionListener(this);// 为文本框注册Action事件监听器，如果在文本框中按回车键，则该监听器的actionPerformed方法被调用
		
		JButton btnFind2 = new JButton("查找");
		btnFind2.setFont(new Font("",Font.BOLD,15));
		btnFind2.addActionListener(this);//为“查找”按钮注册Action事件监听器，如果该按钮被点击，则该监听器的actionPerformed方法被调用
		panel.add(btnFind2);
		
		readerPanel = new ReaderPanel();
		readerPanel.setVisible(false);
		
		
		
		btnFind2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				findByNameToTable();
			}
		});
		
		
		

	}
	
	private void findByNameToTable() {
		String name = readerName.getText();// 获取readerId文本框的内容
		if (name != null) {
			name = name.trim();
			if (!name.equals("")) {
				List<Reader> readerList = readerDao.findByName(name);

		if (readerList != null) {// 如果列表不为空
			// 将列表转换成二维对象数组
			readerData = new Object[readerList.size()][tableHeader.length];
			for (int i = 0; i < readerData.length; i++) {
				readerData[i][0] = readerList.get(i).getId();
				readerData[i][1] = readerList.get(i).getPassword();
				readerData[i][2] = readerList.get(i).getName();
				readerData[i][3] = readerList.get(i).getSex();
				readerData[i][4] = readerList.get(i).getBirth();
				readerData[i][5] = readerList.get(i).getPro();
				readerData[i][6] = readerList.get(i).getGrade();
				readerData[i][7] = readerList.get(i).getTel();
				readerData[i][8] = readerList.get(i).getEmail();
			}
			DefaultTableModel defaultTableModel = new DefaultTableModel(readerData, tableHeader);// 生成数据模型
			table.setModel(defaultTableModel);// 设置表格的数据模型
			table.revalidate();
			table.repaint();
		}
		}
	}
		readerName.setText("");// 清除tfID文本框中的内容，以备再次输入
	}
	
	/*private void findByIdToTable() {
		String id = readerName.getText();// 获取readerId文本框的内容
		if (id != null) {
			id = id.trim();
			if (!id.equals("")) {
				Reader reader = readerDao.findById(id);
				List<Reader> readerList = readerDao.rsToReader((ResultSet) reader);

		if (readerList != null) {// 如果列表不为空
			// 将列表转换成二维对象数组
			readerData = new Object[readerList.size()][tableHeader.length];
			for (int i = 0; i < readerData.length; i++) {
				readerData[i][0] = readerList.get(i).getId();
				readerData[i][1] = readerList.get(i).getPassword();
				readerData[i][2] = readerList.get(i).getName();
				readerData[i][3] = readerList.get(i).getSex();
				readerData[i][4] = readerList.get(i).getBirth();
				readerData[i][5] = readerList.get(i).getPro();
				readerData[i][6] = readerList.get(i).getGrade();
				readerData[i][7] = readerList.get(i).getTel();
				readerData[i][8] = readerList.get(i).getEmail();
			}
			DefaultTableModel defaultTableModel = new DefaultTableModel(readerData, tableHeader);// 生成数据模型
			table.setModel(defaultTableModel);// 设置表格的数据模型
			table.revalidate();
			table.repaint();
						
		} else {					
			JOptionPane.showMessageDialog(this, "你要查找的id为\"" + readerId.getText() + "\"的记录不存在。","查找失败",JOptionPane.ERROR_MESSAGE);
			
		}
		}
	}
	}*/

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		
	}


}
